/**
 * Created by zhaojm on 15/3/21.
 */
game.MyLoaderLayer = cc.LayerColor.extend({

    _loadingBar : null,
    _label : null,
    _spriteSheet : null,

    _startitem : null,

    ctor:function(){
        this._super(new cc.Color(43, 43, 43));

        that = this;

        cc.spriteFrameCache.addSpriteFrames(loaderRes.loader_plist);
        this._spriteSheet = new cc.SpriteBatchNode(loaderRes.loader_png);
        this.addChild(this._spriteSheet);

        if(game.Config.language == game._Enum.language.cn) {
            var www = new cc.Sprite('#59600.png');
            www.setPosition(cc.p(cc.winSize.width * 0.95, cc.winSize.height * 0.95));
            www.setAnchorPoint(cc.p(1, 1));
            this._spriteSheet.addChild(www);
        }


        var logo = new cc.Sprite('#logo.png');
        logo.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.7));
        this._spriteSheet.addChild(logo);


        //var load_bar_bg = new cc.Sprite('#loadbar_bg.png');
        //this._spriteSheet.addChild(load_bar_bg);
        //load_bar_bg.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height * 0.4));
        //
        //
        //this._loadingBar = new cc.Sprite('#loadbar_front.png');
        //this._spriteSheet.addChild(this._loadingBar);
        //this._loadingBar.setAnchorPoint(cc.p(0, 0.5));
        //this._loadingBar.setPosition(cc.p(cc.winSize.width / 2 - this._loadingBar.getContentSize().width / 2, cc.winSize.height * 0.4));
        //this._loadingBar.setScaleX(0);

        if(game.Config.language == game._Enum.language.cn) {
            var weixin = cc.LabelTTF.create('微信服务号：', 'Arial', 24);
            weixin.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.05));
            this.addChild(weixin);
            weixin.setAnchorPoint(cc.p(0.5, 0.5));
        }

        this._label = cc.LabelTTF.create('0%', 'Arial', 24);
        this._label.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.3));
        this.addChild(this._label);
        this._label.setAnchorPoint(cc.p(0.5, 0.5));
        this._label.setColor(new cc.Color(155, 222, 17));

        this._loadingBar = new cc.Sprite('#loader_bar/0.png');
        this._spriteSheet.addChild(this._loadingBar);
        this._loadingBar.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.3));

        this._loadingBar.runAction(
            new cc.RepeatForever(
                new cc.Animate(
                    new cc.Animation(
                        [0, 1, 2, 3, 4, 5].map(function (i) {
                            return cc.spriteFrameCache.getSpriteFrame("loader_bar/" + i + ".png");
                        }), 0.15
                    )
                )
            )
        );


        var startframe = cc.spriteFrameCache.getSpriteFrame('play.png');
        this._startitem = new cc.MenuItemImage(startframe, startframe, startframe, this.onMenuCallback, this);
        this._startitem.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.3);
        //this._startitem.setVisible(false);
        this._startitem.runAction(new cc.FadeOut(0.1));

        var menu = new cc.Menu();
        menu.addChild(this._startitem);
        this.addChild(menu);
        menu.setPosition(0, 0);


        cc.loader.load(g_resources,
            function (result, count, loadedCount) {
                var percent = (loadedCount / count * 100) | 0;
                percent = Math.min(percent, 100);
                that._label.setString(percent + "%");
                //self.loadingBar.setPercentage(percent * 100);
                //that._loadingBar.setScaleX(percent / 100);


            }, function () {
                that._label.setString("100%");
                //that._startitem.setVisible(true);
                //that._loadingBar.stopAllActions();
                //that._loadingBar.setVisible(false);

                //cc.director.runScene(new game.HelloScene());

                that._loadingBar.runAction(
                    new cc.Sequence(
                        new cc.FadeOut(2),
                        new cc.CallFunc(that.onLoadingBarFadeIn)
                    )
                );

                that._label.runAction(
                    //new cc.Sequence(
                        new cc.FadeOut(2)
                        //new cc.CallFunc(that.onLoadingBarFadeIn)
                    //)
                );


            });
    },

    onLoadingBarFadeIn : function(){
        //that._startitem.setVisible(true);
        that._startitem.runAction(new cc.FadeIn(2));
    },


    onMenuCallback:function(){
        game.init();
        cc.director.runScene(new game.HelloScene());
    },




});